package com.itheima.servlet.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Syslog;
import com.itheima.service.system.SyslogService;
import com.itheima.service.system.impl.SyslogServiceImpl;
import com.itheima.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * 日志管理
 */
@WebServlet("/pages/syslog/*")
public class SyslogServlet extends BaseServlet {
    //创建service对象
        private SyslogService ss = new SyslogServiceImpl();
        //查询所有，带分页
    protected void listPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //把方法名传到前端，根据方法名确定是查询全部还是模糊查询
        req.setAttribute("method","listPage");
        //获取前端页面传来的分页数据
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        //根据分页获取全部数据
        List<Syslog> list = ss.findAll(pageNum,pageSize);
        //添加到分页插件中
        PageInfo<Syslog> pageInfo = new PageInfo<>(list);
        //加入会话域传回前端
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/system/syslog/syslog-list.jsp").forward(req,resp);
    }
    //根据id删除
    protected void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取id
        String id = req.getParameter("id");
        //传入service层
       ss.deleteById(id);
        listPage(req,resp);
    }

    //获取excel表格
    protected void toExport(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //返回的数据类型为文件xlsx类型
        //设置可以起中文名
        resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = new String("访问日志.xlsx".getBytes(),"iso8859-1");
        resp.addHeader("Content-Disposition","attachment;fileName="+fileName);
        //获取传输流对象
        ByteArrayOutputStream os = ss.findExport();
        //写回前端
        ServletOutputStream sos = resp.getOutputStream();
        os.writeTo(sos);
        sos.flush();
        os.close();
    }

    //模糊查询
    protected void findByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取方法名，传入前端，切换分页条数
        req.setAttribute("method","findByName");
       //获取输入框的值，切换分页条数有数据
        String name = req.getParameter("name");
        //保证点击下一页时依然有输入的值
        req.setAttribute("name",name);
        //获取前端页面传来的分页数据
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        //根据分页获取全部数据
        List<Syslog> list = ss.findLike(pageNum,pageSize,name);
        //添加到分页插件中
        PageInfo<Syslog> pageInfo = new PageInfo<>(list);
        //加入会话域传回前端
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/system/syslog/syslog-list.jsp").forward(req,resp);
    }

}
